package cn.tedu.straw.portal.controller;


import cn.tedu.straw.portal.dto.PostQuestionDTO;
import cn.tedu.straw.portal.ex.FileEmptyException;
import cn.tedu.straw.portal.ex.FileSizeException;
import cn.tedu.straw.portal.ex.FileTypeException;
import cn.tedu.straw.portal.ex.ParamValidationException;
import cn.tedu.straw.portal.model.Question;
import cn.tedu.straw.portal.security.LoginUserInfo;
import cn.tedu.straw.portal.service.IQuestionService;
import cn.tedu.straw.portal.util.R;
import cn.tedu.straw.portal.vo.QuestionSimpleListItemVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/portal/question")
public class QuestionController {
    @Value("${project.fileupload.question-image.base-dir}")
    String uploadBaseDir;
    @Value("${project.fileupload.question-image.max-size}")
    long uploadMaxSize;
    @Value("${project-fileupload.question-image.content-types}")
    List<String> contentTypes;
    @Autowired
    IQuestionService questionService;
    @RequestMapping("/post")
    public R post(@Valid PostQuestionDTO postQuestionDTO,
                  BindingResult bindingResult,
                  @AuthenticationPrincipal LoginUserInfo loginUserInfo){
        if(bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errorMessage);
        }
        Integer userId = loginUserInfo.getId();
        String userNickName = loginUserInfo.getNickName();
        questionService.post(postQuestionDTO,userId,userNickName);
        return R.ok();
    }

    @PostMapping("/upload/image")
    public R uploadImage(MultipartFile imageFile) {
        //检查上传文件是否为空
        if(imageFile.isEmpty()){
            throw new FileEmptyException("上传图片失败!请选择有效的图片文件");
        }
        //检查上传文件的大小
        if(imageFile.getSize() > uploadMaxSize){
            throw new FileSizeException("上传图片失败!不允许上传超过" + uploadMaxSize / 1024 + "KB的文件!");
        }
        //检查上传的文件类型
        if(!contentTypes.contains(imageFile.getContentType())){
            throw new FileTypeException("上传图片失败!允许上传的图片类型有："+contentTypes);
        }
        //保存上传时使用的文件夹
        String yearAndMonth = DateTimeFormatter.ofPattern("yyyy/MM").format(LocalDate.now());
        File parent = new File(uploadBaseDir,yearAndMonth);
        if(!parent.exists()){
            parent.mkdirs();
        }

        //保存上传文件时使用文件夹
        String filename = System.currentTimeMillis() + "_" +System.nanoTime();
        String originalFilename = imageFile.getOriginalFilename();
        int beginIndex = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(beginIndex);
        String child =filename + suffix;

        //保存上传文件的File对象
        File dest = new File(parent, child);
        //执行保存
        try {
            imageFile.transferTo(dest);
        }catch (IOException e){
            e.printStackTrace();
        }
        //响应
        String url ="http://localhost:8080/" + yearAndMonth + "/" +child;
        System.out.println("url="+url);
        return R.ok(url);
    }
    //http://localhost:8080/portal/question/most-hits
    @GetMapping("/most-hits")
    public R<List<QuestionSimpleListItemVO>> getMostHitsList(){
        return R.ok(questionService.getMostHitList());
    }
    //http://localhost:8080/portal/question/my
    @GetMapping("/my")
    public R<PageInfo<Question>> getMyQuestions(@AuthenticationPrincipal  LoginUserInfo loginUserInfo, Integer page){
        if(page == null || page < 1){
            page = 1;
        }
        Integer userId = loginUserInfo.getId();
        PageInfo<Question> pageInfo = questionService.getMyQuestions(userId,page);
        System.out.println(pageInfo);
        return R.ok(pageInfo);
    }
    //http://localhost:8080/portal/question/10
    @GetMapping("/{id}")
    public R<Question> getQuestionDetails(@PathVariable("id") Integer id){
        Question question = questionService.getQuestionDetails(id);
        return R.ok(question);
    }
}
